<?php
defined('BASEPATH') or exit('No direct script access allowed');

class InteractionType extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('TopicInteractionType');
    }

    public function index()
    {
        $this->load->view('source/index');
    }

    public function showList()
    {
        $result = $this->TopicInteractionType->find(array('is_valid'=>true));
        $data['interactionType'] = $result;
        $this->load->view('interactionType/interactionTypeList', $data);
    }

    public function addInteractionType(){
        $this->load->view('interactionType/addInteractionType');
    }
    public function saveAdd(){
        $insert = array(
            'name'=>$_REQUEST['name'],
            'remark'=>$_REQUEST['remark'],
            'update_time'=> time(),
            'is_valid'=>true
        );
        $result = $this->TopicInteractionType->insert($insert);
        if($result){
            $data['code'] = 1;
            $data['msg'] = '添加成功';
        } else {
            $data['code'] = 0;
            $data['msg'] = '添加失败';
        }
        echo json_encode($data);
    }
            
    function editInteractionType($id)
    {
        $result = $this->TopicInteractionType->findOne(array('_id'=>new MongoId($id)));
        $data['interactionType'] = $result;
        $this->load->view('interactionType/editInteractionType', $data);
    }
    
    public function saveEdit($id){
        $updateData = array(
            'name'=>$_REQUEST['name'],
            'remark'=>$_REQUEST['remark'],
            'update_time'=> time(),
        );
        $result = $this->TopicInteractionType->update(array('_id'=>new MongoId($id)), $updateData);
        if($result){
            $data['code'] = 1;
            $data['msg'] = '修改成功';
        } else {
            $data['code'] = 0;
            $data['msg'] = '修改失败';
        }
        echo json_encode($data);
    }
    
    public function getInteractionType(){
        $result = $this->TopicInteractionType->find(array('is_valid'=>true));
        if($result){
            $data['data'] = $result;
            $data['code'] = 1;
            $data['msg'] = '查询成功';
        } else {
            $data['code'] = 0;
            $data['msg'] = '查询失败';
        }
        echo json_encode($data);
    }
}
